<template>
	<view>
		<!-- 头部 -->
		<yHeader :current='-1' :screenWidth="screenWidth" @Tabs="handleTabs"></yHeader>
		<view class="__inner py-4">
			<view class="flex flex-column w-100 mb-4">
				<!-- 标头 -->
				<view class="flex-shrink bg-lightbrown mb-2" style="height: 88rpx;">
					<view class="flex justify-center align-center bg-white shadow border-top box"
						:class="'border-'+ThemeColors" style="width: 272rpx;height: 100%;">
						<text class="font ">提货人信息</text>
					</view>
				</view>
				<view class="flex w-100 my-2 p-3" style="border: 2rpx solid #bbb;box-sizing: border-box;">
					<el-form class="flex align-center" ref="form" :model="sales_order" label-width="80px">
						<el-form-item style="margin-bottom: 0;" label="提货人:">
							<el-input placeholder="请输入提货人姓名" v-model="sales_order.Sales_Contacts"></el-input>
						</el-form-item>
						<el-form-item style="margin-bottom: 0;" label="手机号">
							<el-input placeholder="请输入提货人手机号" v-model="sales_order.Sales_Phone"></el-input>
						</el-form-item>
						<el-form-item style="margin-bottom: 0;" label="住址">
							<el-cascader v-model="address" style="width: 80%;" :options="Address" :props="{
							    expandTrigger: 'hover',
							    label: 'text',
							    value: 'text',
							    children: 'children',
							  }" placeholder="请选择地址"></el-cascader>
						</el-form-item>
						<el-form-item style="margin-bottom: 0;" label="详细地址">
							<el-input placeholder="请输入提货人详细地址" v-model="sales_order.Sales_Address"></el-input>
						</el-form-item>
					</el-form>
				</view>
				<!-- 标头 -->
				<view class="flex-shrink bg-lightbrown mb-2" style="height: 88rpx;">
					<view class="flex justify-center align-center bg-white shadow border-top box"
						:class="'border-'+ThemeColors" style="width: 272rpx;height: 100%;">
						<text class="font ">订单明细</text>
					</view>
				</view>
				<view class="flex justify-center w-100 my-2 p-3"
					style="border: 2rpx solid #bbb;box-sizing: border-box;">
					<el-table :data="sales_order.Children.sales_order_detail" border style="width: 100%">
						<el-table-column align="center" label="商品图片" width="180">
							<template v-slot="{row}">
								<image style="width: 100rpx;height: 100rpx;" :src="Domain_IMG + row.Article_URL"
									mode=""></image>
							</template>
						</el-table-column>
						<el-table-column align="center" prop="Article_Name" label="商品名称" width="180">
						</el-table-column>
						<el-table-column align="center" prop="Sales_Unit_Price" label="商品单价">
						</el-table-column>
						<el-table-column align="center" prop="Quantity" label="下单数量">
						</el-table-column>
						<el-table-column align="center" label="总金额">
							<template v-slot="{row}">
								<text>{{Number(row.Sales_Unit_Price)*Number(row.Quantity)}}</text>
							</template>
						</el-table-column>
					</el-table>
				</view>
				<!-- 标头 -->
				<view class="flex-shrink bg-lightbrown mb-2" style="height: 88rpx;">
					<view class="flex justify-center align-center bg-white shadow border-top box"
						:class="'border-'+ThemeColors" style="width: 272rpx;height: 100%;">
						<text class="font ">支付</text>
					</view>
				</view>
				<view class="flex justify-center w-100 my-2 p-3"
					style="border: 2rpx solid #bbb;box-sizing: border-box;">
					<view class="flex w-100" style="justify-content: space-evenly;">
						<el-radio-group class="w-100" v-model="radioValue">
							<view class="flex w-100" style="justify-content: space-evenly;">
								<view v-for="(item,i) in PayWay" :key="i"
									class="flex flex-column justify-center align-center">
									<el-radio :label="item.id">{{item.label}}</el-radio>
									<image style="width: 150rpx;height: 150rpx;" :src="item.src" mode="">
									</image>
								</view>
							</view>
						</el-radio-group>
					</view>
					<view class="flex flex-column" style="justify-content: space-between;">
						<view class="">

						</view>
						<el-button :loading="loading" size="small" @click="Pay()" type="primary">支付</el-button>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部 -->
		<yFooter :screenWidth="screenWidth"></yFooter>
		<PayMask @show="shows" :pay="pay" :payURL="payURL"></PayMask>
	</view>
</template>

<script>
	const app = getApp();
	import {
		CityData
	} from "@/static/js/city.js";
	const {
		Address
	} = CityData();
	import yHeader from '@/components/yHeader.vue'
	import yFooter from '@/components/yFooter.vue'
	import PayMask from '@/components/PayMask.vue'
	import times from '@/static/js/utils.js'
	import {
		WX2CheckStockAction
	} from '@/api/WX.js'
	import {
		getAddAction
	} from '@/api/global.js'
	import {
		GroupWeChatNativeAction
	} from '@/api/group.js'
	import {
		Message
	} from 'element-ui'
	export default {
		components: {
			yHeader,
			yFooter,
			PayMask
		},
		data() {
			return {
				loading:false,
				radioValue: 1,
				show: false,
				pay: false,
				payURL: "",
				screenWidth: uni.getSystemInfoSync().windowWidth,
				ThemeColors: app.globalData.ThemeColors,
				Domain_IMG: app.globalData.Domain_IMG,
				Domain_Business: app.globalData.Domain_Business,
				Address: Address(),
				form: {
					name: '',
					phone: '',
					address: '',
					addressDetail: ''
				},
				address: [],
				sales_order: {
					Sales_Order_SerialNumber: '',
					Store_SerialNumber: '',
					Sales_Date: times.nowtime(),
					Open_ID: uni.getStorageSync('loginInfo').use,
					Sales_Amount: '',
					Sales_Contacts: uni.getStorageSync('loginInfo').username,
					Sales_Phone: uni.getStorageSync('loginInfo').use,
					Sales_Address: '',
					Sales_Message: '',
					Collection_Amount: 0,
					Sales_ID: 'xiaoshou1',
					Receive_Type: 0,
					Refund_Type: 0,
					App_ID: 'wxd96a1f0fbc125263',
					Enterprise_Info_SerialNumber: uni.getStorageSync('loginInfo').enterprise,
					Children: {
						sales_order_detail: [{
							Sales_Order_Detail_SerialNumber: '',
							Sales_Order_SerialNumber: '',
							Sales_Unit_Price_Stock_SerialNumber: '',
							Article_SerialNumber: '',
							Article_Name: '',
							Sign: '无货货主',
							Quantity: '',
							Unit_Price: '',
							Sales_Unit_Price: '',
							Amount: '',
							Receive_Type: 0,
							Refund_Type: 0,
							Enterprise_Info_SerialNumber: uni.getStorageSync('loginInfo').enterprise,
							Article_Specifications_SerialNumber: '',
							Article_URL: '',
						}]
					}
				},
				PayWay: [{
						id: 1,
						label: '微信支付',
						src: '../../static/icon/WXlogo.png'
					},
					{
						id: 2,
						label: '支付宝支付',
						src: '../../static/icon/zfb.png'
					},
				],
			};
		},
		methods: {
			handleTabs(url) {
				uni.navigateTo({
					url
				})
			},
			async Pay() {
				if(this.sales_order.Sales_Phone==''){
					Message({
						type:'error',
						message:'请填写手机号'
					})
					return
				}
				if(this.sales_order.Sales_Contacts==''){
					Message({
						type:'error',
						message:'请填写联系人'
					})
					return
				}
				if(this.address.length==0){
					Message({
						type:'error',
						message:'请填写收货地址'
					})
					return
				}
				if(this.sales_order.Sales_Address==''){
					Message({
						type:'error',
						message:'请填写详细的收货地址'
					})
					return
				}
				uni.showLoading({
					title:'提交中...'
				})
				this.loading = true
				const res = await WX2CheckStockAction({
					data: {
						Data: JSON.stringify([{
							Sales_Unit_Price_Stock_SerialNumber: this.sales_order.Children
								.sales_order_detail[0].Sales_Unit_Price_Stock_SerialNumber,
							Quantity: this.sales_order.Children.sales_order_detail[0].Quantity,
						}])
					}
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else if (res.Status == 'insufficient') {
					this.loading = false
					uni.hideLoading()
					Message({
						type:'error',
						message:'该商品暂时库存不足,无法购买'
					})
					return
				}else if (res.Status == 'success'){
					console.log(res)
					this.sales_order.Sales_Address = this.address[0]+'/'+this.address[1]+'/'+this.address[2]+'/'+this.sales_order.Sales_Address
					let data = {
						Data: JSON.stringify({
							sales_order: [this.sales_order]
						}),
						token:uni.getStorageSync('loginInfo').token,
					}
					this.getAdd(data)
				}
			},
			async getAdd(data){
				const res = await getAddAction({
					data
				})
				console.log(res)
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.removeStorageSync('loginInfo')
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}else if(res.Status =='success'){
					this.PaySrc(res.Data)
				}
			},
			async PaySrc(Sales_Order_SerialNumber){
				let data = {
						Data:Sales_Order_SerialNumber,
						Collection_Type:'微信支付',
						token:uni.getStorageSync('loginInfo').token,
						Order_Type:'1'
					}
				const res = await GroupWeChatNativeAction({
					data
				})
				console.log(res)
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else if(res.result_msg=='SUCCESS'){
					this.loading = false
					uni.hideLoading()
					this.payURL = res.code_url
					this.pay = true
				}else{
					this.loading = false
					uni.hideLoading()
					Message({
						type:'error',
						message:'提交失败,请稍后重试...'
					})
				}
			},
			shows(e) {
				this.pay = e
			},
			accMul(arg1, arg2) { //浮点型相乘
				let m = 0,
					s1 = arg1.toString(),
					s2 = arg2.toString();
				try {
					m += s1.split(".")[1].length
				} catch (e) {}
				try {
					m += s2.split(".")[1].length
				} catch (e) {}
				return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
			},
		},
		onLoad(e) {
			console.log(uni.getStorageSync('GoodsDetail'))
			this.sales_order.Store_SerialNumber = e.Store_SerialNumber
			this.sales_order.Article_SerialNumber = e.Article_SerialNumber
			this.sales_order.Children.sales_order_detail[0].Article_Specifications_SerialNumber = e
				.Article_Specifications_SerialNumber
			this.sales_order.Children.sales_order_detail[0].Article_URL = uni.getStorageSync('GoodsDetail').Picture_URL
			this.sales_order.Sales_Amount = this.accMul(uni.getStorageSync('Goodsprice'), uni.getStorageSync('Quantity'))
			this.sales_order.Children.sales_order_detail[0].Sales_Unit_Price = uni.getStorageSync('Goodsprice')
			this.sales_order.Children.sales_order_detail[0].Article_SerialNumber = e.Article_SerialNumber
			this.sales_order.Children.sales_order_detail[0].Quantity = uni.getStorageSync('Quantity')
			this.sales_order.Children.sales_order_detail[0].Amount = this.accMul(uni.getStorageSync('Goodsprice'), uni
				.getStorageSync('Quantity'))
			this.sales_order.Children.sales_order_detail[0].Sales_Unit_Price_Stock_SerialNumber = e
				.Sales_Unit_Price_Stock_SerialNumber
			this.sales_order.Children.sales_order_detail[0].Unit_Price = uni.getStorageSync('GoodsDetail').Unit_Price
			this.sales_order.Children.sales_order_detail[0].Article_Name = uni.getStorageSync('GoodsDetail').Article_Name
			console.log(this.sales_order)
		}
	}
</script>

<style lang="scss">

</style>